package sprintServer.start;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import sprintServer.dbconnect.Conn;
import sprintServer.model.FriendModel;

public class FriendInfo {
	StringBuffer sql = new StringBuffer();
	Connection con ;
	PreparedStatement pstmt;
	ResultSet rs;
	ArrayList<FriendModel> friendList = new ArrayList<FriendModel>();
	
	@SuppressWarnings("null")
	public ArrayList<FriendModel> getFriend(String id){
		String myfriend;
		String [] mfList = null;
		sql.append("select friendId from join");
		sql.append("where id = ? ");
		try{
			con = Conn.getConnection();
			pstmt = con.prepareStatement(sql.toString());
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				myfriend = rs.getString("friendId");
				mfList = myfriend.split(",");
			}
			if(mfList.length > 0){
				for(int i=0; i<mfList.length;i++){
					StringBuffer sql2 = null ;
					sql2.append("select state from join");
					sql2.append("where id = ?");
					pstmt = con.prepareStatement(sql2.toString());
					pstmt.setString(1, mfList[i]);
					rs = pstmt.executeQuery();
					while(rs.next()){
						FriendModel fm = new FriendModel();
						fm.setFriendName(mfList[i]);
						fm.setFriendState(rs.getInt("state"));
						friendList.add(fm);
					}
				}
			}
			
		}catch(SQLException se){}
		finally{
			try{if(rs != null)rs.close();}catch	(SQLException se1){}
			try{if(pstmt != null)pstmt.close();}catch	(SQLException se1){}
			try{if(con != null)con.close();}catch	(SQLException se1){}			
		}
		return friendList;
	}
}
